package cn.zsssgjcx.hjsg.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.zsssgjcx.hjsg.R;
import cn.zsssgjcx.hjsg.model.CityEntity;
import cn.zsssgjcx.hjsg.utils.ToastUtils;

public class CityListAdapter extends BaseAdapter {
    private Context context;

    protected List<CityEntity> curCityList = new ArrayList<>();
    private List<CityEntity> totalCityList;
    private List<CityEntity> hotCityList;
    private LayoutInflater inflater;
    final int VIEW_TYPE = 3;
    private String locationCity;
    private String curSelCity;

    private HashMap<String, Integer> alphaIndexer;// 存放存在的汉语拼音首字母和与之对应的列表位置
    public CityListAdapter(Context context, List<CityEntity> totalCityList, List<CityEntity> hotCityList) {
        this.context = context;
        this.totalCityList = totalCityList;
        this.hotCityList = hotCityList;
        inflater = LayoutInflater.from(context);

        //alphaIndexer = new HashMap<>();

//        for (int i = 0; i < totalCityList.size(); i++) {
//            // 当前汉语拼音首字母
//            String currentStr = totalCityList.get(i).getKey();
//
//            String previewStr = (i - 1) >= 0 ? totalCityList.get(i - 1).getKey() : " ";
//            if (!previewStr.equals(currentStr)) {
//                String name = getAlpha(currentStr);
//                alphaIndexer.put(name, i);
//            }
//        }
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE;
    }

    @Override
    public int getItemViewType(int position) {
        return position < 2 ? position : 2;
    }

    @Override
    public int getCount() {
        return totalCityList == null ? 0 : totalCityList.size();
    }

    @Override
    public Object getItem(int position) {
        return totalCityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final TextView curCityNameTv;
        ViewHolder holder;
        int viewType = getItemViewType(position);
        if (viewType == 0) { // 定位
            convertView = inflater.inflate(R.layout.select_city_location_item, null);

            LinearLayout noLocationLl = (LinearLayout) convertView.findViewById(R.id.cur_city_no_data_ll);
            TextView getLocationTv = (TextView) convertView.findViewById(R.id.cur_city_re_get_location_tv);
            curCityNameTv = (TextView) convertView.findViewById(R.id.cur_city_name_tv);

            if (TextUtils.isEmpty(locationCity)) {
                noLocationLl.setVisibility(View.VISIBLE);
                curCityNameTv.setVisibility(View.GONE);
                getLocationTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //initLocation();
                    }
                });
            } else {
                noLocationLl.setVisibility(View.GONE);
                curCityNameTv.setVisibility(View.VISIBLE);

                curCityNameTv.setText(locationCity);
                curCityNameTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!locationCity.equals(curSelCity)) {
                            //设置城市代码
                            String cityCode = "";
                            for (CityEntity cityEntity : curCityList) {
                                if (cityEntity.getName().equals(locationCity)) {
                                    cityCode = cityEntity.getCityCode();
                                    break;
                                }
                            }
                            showSetCityDialog(locationCity, cityCode);
                        } else {
                            ToastUtils.show("当前定位城市" + curCityNameTv.getText().toString());
                        }
                    }
                });
            }
        } else if (viewType == 1) { //热门城市
            convertView = inflater.inflate(R.layout.recent_city_item, null);
            GridView hotCityGv = (GridView) convertView.findViewById(R.id.recent_city_gv);
            hotCityGv.setAdapter(new HotCityListAdapter(context, this.hotCityList));
            hotCityGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    CityEntity cityEntity = hotCityList.get(position);
                    showSetCityDialog(cityEntity.getName(), cityEntity.getCityCode());
                }
            });
        } else {
            if (null == convertView) {
                convertView = inflater.inflate(R.layout.city_list_item_layout, null);
                holder = initView(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            CityEntity cityEntity = totalCityList.get(position);
            holder.cityKeyTv.setVisibility(View.VISIBLE);
            holder.cityKeyTv.setText(getAlpha(cityEntity.getKey()));
            holder.cityNameTv.setText(cityEntity.getName());

            if (position >= 1) {
                CityEntity preCity = totalCityList.get(position - 1);
                if (preCity.getKey().equals(cityEntity.getKey())) {
                    holder.cityKeyTv.setVisibility(View.GONE);
                } else {
                    holder.cityKeyTv.setVisibility(View.VISIBLE);
                }
            }
        }

        return convertView;
    }

    private ViewHolder initView(View v){
        ViewHolder holder = new ViewHolder();
        holder.cityNameTv = v.findViewById(R.id.city_key_tv);
        holder.cityKeyTv = v.findViewById(R.id.city_name_tv);
        return holder;
    }

    private class ViewHolder {
        TextView cityNameTv;
        TextView cityKeyTv;
    }

    private String getAlpha(String key) {
        if (key.equals("0")) {
            return "定位";
        } else if (key.equals("1")) {
            return "热门";
        } else {
            return key;
        }
    }

    /**
     * 展示设置城市对话框
     */
    private void showSetCityDialog(final String curCity, final String cityCode) {
        if (curCity.equals(curSelCity)) {
            ToastUtils.show("当前定位城市" + curCity);
            return;
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(context);  //先得到构造器
        builder.setTitle("提示"); //设置标题
        builder.setMessage("是否设置 " + curCity + " 为您的当前城市？"); //设置内容

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                //选中之后做你的方法

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        builder.create().show();
    }

}
